<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/4/2
 * Time: 11:35
 */
declare(strict_types=1);

namespace App\Service;
use App\Model\Dao\TradeOrderForceSuccessSingleDao;
use App\Utils\CustomUtils;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Exception;

/**
 * Class SettleTradeOrderForceSuccessService
 * @package App\Service
 * 交易订单补单结算
 */
class SettleTradeOrderForceSuccessService extends CommonService
{
    /**
     * @Inject()
     * @var TradeOrderForceSuccessSingleDao
     */
    private $TradeOrderForceSuccessSingleDao;

    public function handle($param) {
        $forceSuccessOrder = $this->TradeOrderForceSuccessSingleDao->getByOrderId($param['orderId']);
        if (empty($forceSuccessOrder)) {
            throw new Exception('查询不到补单数据', FAILED_CODE);
        }
        if($forceSuccessOrder['handleStatus'] != $param['handleStatus']) {
            throw new Exception('补单处理状态不对应', FAILED_CODE);
        }
        if($forceSuccessOrder['settleStatus'] == 1) {
            throw new Exception('补单已结算完成', FAILED_CODE);
        }

        $this->TradeOrderForceSuccessSingleDao->update($forceSuccessOrder['id'], [
            'settleStatus'  => 1
        ]);

        // 如果是二审通过，写一条mq到交易成功
        if ($param['handleStatus'] == 3) {
            $this->addMq([
                'accessType'   => ACCESS_TYPE_TRADE_ORDER,
                'mchAccountId' => $param['mchAccountId'],
                'orderId'      => $param['orderId'],
            ]);
        }
    }

}